﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace WinFormRapor
{
    public partial class Login : Form
    {
        string appPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\Config.ini";

        public Login()
        {
            InitializeComponent();
            this.Load += new EventHandler(Login_Load);
        }

        void Login_Load(object sender, EventArgs e)
        {
            txtDB.Text = Config.Get.DbName;
            txtFirmaKod.Text = Config.Get.FirmaKod;
            txtPass.Text = Config.Get.Pass;
            txtServer.Text = Config.Get.Server;
            txtUser.Text = Config.Get.User;
        }

        public void BinSerialize(string sFileName)
        {
            FileStream fs;
            if (File.Exists(appPath))
                fs = new FileStream(sFileName, FileMode.Truncate);
            else
                fs = new FileStream(sFileName, FileMode.OpenOrCreate);

            BinaryFormatter formatter = new BinaryFormatter();
            try
            {
                formatter.Serialize(fs, Config.Get);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnGir_Click(object sender, EventArgs e)
        {
            Config.Get.DbName = txtDB.Text;
            Config.Get.FirmaKod = txtFirmaKod.Text;
            Config.Get.Pass = txtPass.Text;
            Config.Get.Server = txtServer.Text;
            Config.Get.User = txtUser.Text;

            BinSerialize(appPath);
            base.DialogResult = DialogResult.OK;
        }
    }
}
